<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Vote extends CI_Controller {
    
    function __construct() {
        parent::__construct();
    }

	public function add_vote(){
		$user_id = $this->input->post('user_id');
		if(!$user_id){
			api_failed('empty_user_id');
		}
		$product_id = $this->input->post('product_id');
		if(!$product_id){
			api_failed('empty_product_id');
		}

        $this->load->model('vote_model');
        if($this->vote_model->has_vote($user_id, $product_id)){
        	api_failed('has_vote');
        }

        $data = array(
        	'vote'=>$this->vote_model->add_vote($user_id, $product_id));
        api_success($data);
	}

	public function del_vote(){
		api_failed('invalid operate');
		$vote_id = $this->input->post('vote_id');
		if(!$vote_id){
			api_failed('empty_vote_id');
		}
        $this->load->model('vote_model');
        $data = array('vote'=>$this->vote_model->del_vote($vote_id));
        api_success($data);
	}

	public function set_vote(){
		api_failed('invalid operate');
		$vote_id = $this->input->post('vote_id');
		if(!$vote_id){
			api_failed('empty_vote_id');
		}
		$sets = $this->input->post('sets');
        $this->load->model('vote_model');
        $data = array('vote'=>$this->vote_model->set_vote($vote_id, $sets));
        api_success($data);
	}

	public function get_product_vote_list_sum(){
		$product_id = $this->input->get('product_id');
		if(!$product_id){
			api_failed('empty_product_id');
		}
        $this->load->model('vote_model');
        $data = array(
        	'sum'=>$this->vote_model->get_sum_production_vote(
        		$product_id
        	));
        api_success($data);
	}

    public function get_product_vote_list() {
		$product_id = $this->input->get('product_id');
		if(!$product_id){
			api_failed('empty_product_id');
		}
		$start_index = $this->input->get('start_index');
		if(!$start_index){
			api_failed('empty_start_index');
		}
		$step = $this->input->get('step');
		if(!$step){
			api_failed('empty_step');
		}
        $this->load->model('vote_model');
        $data = array(
        	'vote_list'=>$this->vote_model->
        	get_product_votes_list($product_id, null, $start_index, $step),
        	'sum'=>$this->vote_model->get_sum_production_vote(
        		$product_id
        	));
        api_success($data);
    }
}